package com.treasure.mall.biz.entity.order;

import com.treasure.mall.biz.entity.embeddable.GoodsInfo;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.order.OrderEnums;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/11
 */
@Entity
@Table(name = "zb_order_item")
@Setter
@Getter
@Cacheable(false)
public class OrderItemPO {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @ManyToOne
    @JoinColumn(name = "sub_order_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private SubOrderPO subOrderPO;
    /**
     * 商品信息
     */
    private GoodsInfo goodsInfo;
    /**
     * 已经抵扣的钱（比如拍卖的保证金）
     */
    @Column(name = "deduct_amount", scale = 2, precision = 9)
    private BigDecimal deductAmount;
    /**
     * 用户ID
     */
    @Column(name = "user_id")
    private Long userId;
    /**
     * 商品数量
     */
    private int num;
    /**
     * 优惠金额
     */
    @Column(name = "pmt_amount", scale = 2, precision = 9)
    private BigDecimal pmtAmount;
    /**
     * 鉴定服务费
     */
    @Column(name = "identify_fee", scale = 2, precision = 9)
    private BigDecimal identifyFee;
    /**
     * 鉴定机构
     */
    @Column(name = "identify_org_id")
    private int identifyOrg;
    /**
     * 物流状态
     */
    @Column(name = "deliver_status")
    private OrderEnums.DeliverStatus deliverStatus;
    /**
     * 签收时间
     */
    @Column(name = "receive_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime receiveTime;
    /**
     * 发货时间
     */
    @Column(name = "deliver_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime deliverTime;

    /**
     * 售后完成时间
     */
    @Column(name = "afterSale_finish_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime afterSaleFinishTime;
    /**
     * 售后单号
     */
    @Column(name = "after_sale_id")
    private String afterSaleId;
    /**
     * 鉴定状态
     */
    @Column(name = "identify_status")
    private OrderEnums.IdentifyStatus identifyStatus;
    /**
     * 售后状态
     */
    @Column(name = "after_sale_status")
    private OrderEnums.AfterSaleStatus afterSaleStatus;
    /**
     * 一级佣金
     */
    @Column(name = "father_rebate")
    private BigDecimal fatherRebate = BigDecimal.ZERO;
    /**
     * 二级佣金
     */
    @Column(name = "grand_rebate")
    private BigDecimal grandRebate = BigDecimal.ZERO;
    /**
     * 货款
     */
    @Column(name = "payment")
    private BigDecimal payment = BigDecimal.ZERO;

    public String deliverStatusClass() {
        switch (deliverStatus) {
            case RECEIVED:
                return Constant.COLOR_PRIMARY;
            default:
                return Constant.COLOR_WARNING;
        }
    }

    public boolean enableAfterSale() {
        if (afterSaleStatus == OrderEnums.AfterSaleStatus.REFUSE_APPLY) {
            return true;
        }
        if (afterSaleStatus != null) {
            return false;
        }
        if (subOrderPO.getOrderPO().getPayStatus() == OrderEnums.PayStatus.PAYED && deliverStatus != OrderEnums.DeliverStatus.RECEIVED) {
            return true;
        }

        if (deliverStatus == OrderEnums.DeliverStatus.RECEIVED) {
            int daysNum = Period.between(LocalDate.now(), receiveTime.toLocalDate()).getDays();
            if (daysNum < 15) {
                return true;
            }
        }
        return false;
    }

    public BigDecimal goodsAmount() {
        return this.goodsInfo.getPrice().multiply(BigDecimal.valueOf(this.num));
    }

    public BigDecimal refundAmount() {
        return goodsAmount().add(this.identifyFee).subtract(pmtAmount);
    }

    public boolean isAfterSale() {
        return this.afterSaleStatus != null && this.afterSaleStatus != OrderEnums.AfterSaleStatus.COMPLETE_APPLY
                && afterSaleStatus != OrderEnums.AfterSaleStatus.CANCEL_APPLY
                && afterSaleStatus != OrderEnums.AfterSaleStatus.REFUSE_APPLY;
    }

    /**
     * 是否可以转正，
     * 只有不存在售后、售后拒绝、取消售后的才可以转正
     *
     * @return
     */
    public boolean isCanConverted() {
        return this.afterSaleStatus == null || this.afterSaleStatus == OrderEnums.AfterSaleStatus.REFUSE_APPLY || this.afterSaleStatus == OrderEnums.AfterSaleStatus.CANCEL_APPLY;
    }
}
